package com.taikang.common.util;

import org.apache.log4j.Logger;

/**
 * 
 * @author liucr
 * 
 */
public class StringUtils extends org.apache.commons.lang.StringUtils{
	
	protected static final Logger logger = Logger.getLogger(StringUtils.class);
	/**
	 * 格式化long型数据，长度为len，不足前面补0
	 * @param value
	 * @param len
	 * @return
	 */
	public static String formatString(long value,int len){
		String va=String.valueOf(value);
		if(va.length()<len){
			for(int i=0;i<len-va.length();i++){
				va="0"+va;
			}
		}
		return va;
		
	}
	
	/*added by wanghuan
	 * 
	 * 将16进制字串转化为标准字串
	 */
	public static String hexStr2String(String bytestr)  
	//字节流(以字符串表示)
	{
		int ll = bytestr.length();
		if ( ll % 2 != 0) 
		{
			return null;
		} 
		try
		{
			byte bt_file[] = new byte[ll/2];
			for (int i = 0; i < ll/2; i++)
			{
				bt_file[i] = (byte)Integer.parseInt(bytestr.substring(i*2,i*2 + 2),16);
			}
			String content = new String(bt_file,0,bt_file.length,"GBK");
			return content;
		}
		catch (Exception e)
		{
			System.out.println("com.taikang.common.util:"+e);
			return null;
		}
	}
	
	
	//add by fee 20091027
	/*
	 * 手动解析上传的excel文件
	 * 对系统中手动生成的html格式Excel文件有效
	 */
	public static String[][] parse(String context)
	{
		String[] outString;
		String[][] content;
		//掐头
		int start = context.indexOf("title=start>")+ 14;
		if(start!=13)
		{
			context = context.substring(start);
		}
		//去尾
		int end = context.indexOf("<![if supportMisalignedColumns]>");
		if(end!= -1)
		{
			context = context.substring(0,end);
		}
		//利用<tr>标签对字符串进行分组
		outString = context.split("title=tr");
		if(outString.length<1)
		{
			return null;
		}
		content = new String[outString.length-1][];
		for(int i=1;i<outString.length;i++)
		{
			//首先将每个字段trim后利用<td>标签进行再次分组
			content[i-1]= outString[i].trim().split("title=td");
			for(int j = 0 ; j < content[i-1].length ; j++)
			{
				int startA = content[i-1][j].indexOf(">");
				int endA = content[i-1][j].indexOf("<");
				String tag = "";
				while(startA!=-1 && endA!=-1)
				{
					tag += content[i-1][j].substring(startA+1,endA);
					content[i-1][j] = content[i-1][j].substring(endA+1);
					startA = content[i-1][j].indexOf(">");
					endA = content[i-1][j].indexOf("<");
				}
				content[i-1][j] = tag.trim();
			}
		}
		return content;
	}
	
	
	public  static void main(String args[])
	{
		String con="<tr height=17 style='height:12.75pt' title='tr'><td height=17 style='height:12.75pt' title='td'>1</td><td title='td'>V3903402<span style='display:none'>7</span></td><td title='td'>周玉华</td><td>V</td><td align=right x:num title='td'>43</td><td align=right x:num title='td'>79</td><td align=right x:num title='td'>80</td><td colspan=3 style='mso-ignore:colspan'></td></tr>";
	    @SuppressWarnings("unused")
		String[][] content1= parse(con);
//	    System.out.println(content1[0][0]);
	}

}
